package com.zishi.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zishi.model.pojo.Report;
import com.zishi.service.ReportService;
import com.zishi.service.UtilService;
import com.zishi.util.*;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.net.UnknownHostException;

/**
 * @author TP
 * @version 1.0
 * @date 2022/5/9/0009 9:18
 */
@RestController
@Log4j
public class UtilController {

    @Autowired
    private UtilService utilService;


    @Autowired
    private ReportService reportService;


    @RequestMapping("/zishi/upload")
    public Boolean upload(MultipartFile file,Integer id, String openid, Boolean msg) {
        if (file.isEmpty()) {
            log.error("文件为空");
            return false;
        }

        String fileName = utilService.upload(file, openid);
        String fileWebPath = ConstUtil.WEBSITE_IMAGE_PATH + "/" + fileName;
        String databaseFilePath =  fileWebPath + ConstUtil.SEPARATOR;

//        String testFilePath =  ConstUtil.IMAGE_PATH + "/" + fileName;
        if (fileName == null) {
            log.error("文件名为空，文件上传失败");
            return false;
        }
        reportService.updatePicture(id, databaseFilePath);

        System.out.println("11111111: " +msg +"22222222\n");
        if (msg) {
//            String fileWebPath = ConstUtil.WEBSITE_IMAGE_PATH + "/" + filePath;
//            QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("id", id);
//            List<Report> reports = reportService.list(queryWrapper);
            Report report = reportService.getById(id);
            boolean isSquat = false;
            String posture = report.getPostureType();
            if (TrainingEnumUtil.squat.name().equals(posture)) {
                isSquat = true;
            }

            JSONObject json;
            try {

                json =  ConnectionPy.connectionPy(PartitionUtil.separateCharacters(report.getPicture()),
                         ConstUtil.IMAGE_PATH+"/"+openid,posture, isSquat);
            } catch (UnknownHostException e) {
                e.printStackTrace();
                log.error(e);
                return false;
            }

            UpdateWrapper<Report> updateWrapper = new UpdateWrapper<>();
            Report updateReport = new Report();
            updateWrapper.eq("id", id);
            updateReport.setProblem(json.getString("problem"));
            String userImg = TrainingPathChangeUtil.changeLocalPathToWeb(json.getString("userImg"));
            updateReport.setReturnPicture(userImg);

            String userImg2 = "";
            if (isSquat) {
                userImg2 = TrainingPathChangeUtil.changeLocalPathToWeb(json.getString("userImg2"));
                updateReport.setReturnPicture(userImg + ";" + userImg2);
            }
            updateReport.setScore(json.getString("score"));

            reportService.update(updateReport, updateWrapper);

//            reportService.updateTrainingReport(id, json.getString("problem"), json.getString("userImg"),
//                    json.getString("score")==null ? null : Integer.parseInt(json.getString("score")));
        }
        return true;
    }
}
